
	set more off


use Data/ready_accounts_inventory_PV.dta, clear

***************************************
****** BASIC SAMPLE RESTRICTIONS ******
***************************************

	***  keep only households that do not adopt themselves
	drop if ever_solar==1

	*** keep only contract starts with offer names
	drop if green_plan==.

	
***************************
****** MERGE PV DATA ******
***************************
	
	drop year 
	gen year=year(contract_start)
	gen month = month(contract_start)
	gen quarter =1 if month < = 3
	replace quarter =2 if month > 3 & month < =6
	replace quarter = 3 if month > 6 & month <=9
	replace quarter = 4 if month >9 & month <=12

	gen contract_quarter = yq(year,quarter)
	format contract_quarter %tq
	
	* get postcode solar data
	merge m:1 postcode contract_quarter using Data/postcode_quarterinstall.dta

	drop if _merge ==2
	drop _merge

**************************
****** CODE EVENTS *******
**************************

	* events:

	* PFIT commencement Nov 2009
	* PFIT ends December 2011
	* SRES reduced from 5X to 3X July 2011, from 3X to 2X July 2012 , 1X January 2013 
	* TFIT ends December 2012



	by postcode contract_quarter, sort: egen NCustomers = count(account_id)
	by postcode, sort: egen mean_cust = mean(NCustomers)

	gen SHCP_endtime = -1*(contract_quarter  - tq(2009, q3))

	gen PFIT_starttime = contract_quarter  - tq(2009, q4)
	gen PFIT_endtime = -1*(contract_quarter  - tq(2012, q1))

	gen SRES1_endtime =  -1*(contract_quarter  - tq(2011, q3))
	gen SRES2_endtime =  -1*(contract_quarter  - tq(2012, q3))

	gen SRES_TFIT_endtime = -1*(contract_quarter  - tq(2013, q1))
	gen FFIT_endtime = -1*(contract_quarter  - tq(2014, q1))

******************************
****** MERGE CENSUS DATA *****
******************************

	** get pre-event from 2006 census  
	
	merge m:1 postcode using  OrigData/Census2006postcode/PSeparate_postcode.dta   , keepusing(PSeparate Separate TotalHouse)
	
	rename PSeparate  PSep_2006 
	rename Separate  Sep_2006 
	rename TotalHouse TotalH_2006 
	
	drop if _merge ==2
	drop _merge
	*/
	merge m:1 postcode contract_quarter using Data/Census_postcode.dta

	** use all years to interpolate postcode trends
	* census is taken in August, i.e. quarter 3
	
	foreach var in PSeparate Separate TotalHouse median_income median_mortgage median_rent median_age PBachelor PRented median_rooms PFullTime {
		by postcode (contract_quarter), sort: ipolate `var' contract_quarter , gen(`var'_interp)
	}


	
*************************************
*****FURTHER SAMPLE RESTRICTIONS*****
*************************************
	
	* proportion of postcodes with PV
	
	gen Pcum = cum/TotalHouse_interp
	gen Pnew = new/TotalHouse_interp
	
	drop if Sep_2006 ==.
	drop if TotalHouse_interp==.
	drop if Pcum >= 0.6
	drop if mean_weighted==.
	drop if green_plan==.
	drop if _merge ==2
	drop _merge
	
	keep if contract_quarter <=tq(2016q1)
	
save Data/ready_accounts_inventory_PV_dwellingsince2006.dta, replace


**********************************
****** MERGE MODULE PRICES *******
**********************************
	
	merge m:1 contract_quarter using Data/BNEF_module_quarter.dta
	
	drop if _merge ==2
	drop _merge

	
	
**********************************
****** MERGE HOUSE/UNIT PRICES ***
**********************************	
	rename postcode postcode_orig
	*3940 Capel Sound not in house price data - use rosebud 3939

	gen postcode = postcode_orig
	
	replace postcode = 3939 if postcode_orig==3940
	
	merge m:1 postcode contract_quarter using Data/postcodequarter_house_unit_prices.dta
	
	drop if _merge ==2
	drop _merge
	
	drop postcode 
	
	rename postcode_orig postcode

	
**********************************
****** MERGE NETWORK TARIFFS ***
**********************************	
 merge m:1 postcode year using  Data/networktariffs_postcode.dta
	
	drop if _merge == 2
	
	drop _merge 
	
	
	
	

***********************************
****** MERGE BUILDING DATA *******
***********************************


merge m:1 postcode using Data/BuildingType.dta

	drop _merge
	
	

foreach var in ROOFCO ROOFFI ROOFIM ROOFME ROOFSY ROOFTI WALLSA WALLSC WALLSF ///
WALLSM WALLSMB WALLSP WALLSR WALLSS WALLST WALLSV {

	gen P`var' = `var'/RESIDENTIALBUILDINGS
	
	
	
	}	
	

***********************************
****** MERGE AREA DATA *******
***********************************

	
merge m:1 postcode using Data/PostcodeArea.dta

	keep if _merge ==3
	drop _merge
	
	
	
	** rescale and label variables
	
	replace cum = cum/1000
	replace new = new/1000
	replace TotalHouse_interp = TotalHouse_interp/1000
	replace Separate_interp = Separate_interp/1000
	replace median_income_interp =  median_income_interp/1000
	replace median_mortgage_interp =median_mortgage_interp/1000
	replace median_rent_interp = median_rent_interp/1000
	
	by postcode contract_quarter, sort: egen price_green = mean(discounted) if green_plan==1
	by postcode contract_quarter, sort: egen price_nongreen = mean(discounted) if green_plan==0
	
	label variable cum "Solar Panels (000s)"
	label variable mean_weighted "Average electricity tariff  (AUD per kWh)"
	label variable discounted_price "Average electricity price  (AUD per kWh)"
	label variable price_nongreen "Conventional electricity price  (AUD per kWh)"
	label variable TotalHouse_interp "Dwellings (000s)"
	label variable quarter "Time trend"
	 
	label variable median_income_interp "Median Income (AUD 000s)"
	label variable median_mortgage_interp "Median Mortgage Payment (AUD 000s)"
	label variable median_rent_interp "Median Rental Payment (AUD 000s)" 
	label variable PBachelor_interp "Proportion Bachelor's Degree"
	label variable PRented_interp "Proportion Rental Properties"
	label variable median_rooms_interp "Median House Size"
	label variable PFullTime_interp "Proportion Employed Full Time"
	label variable median_age_interp "Median Age"
	
	
	
	label variable PROOFCO "% Roof Concrete"
	label variable PROOFFI "% Roof Fibre Cement"
	label variable PROOFIM "% Roof Imitation Tile"
	label variable PROOFME "% Roof Metal Sheeting"
	label variable PROOFSY "% Roof Synthetic"
	label variable PROOFTI "% Roof Tile"
	
	
save Data/event_study_samplesince2006.dta, replace

*****************************************
****** COLLAPSE TO POSTCODE QUARTER *****
*****************************************

use Data/event_study_samplesince2006.dta, clear

drop NCustomers
	by postcode contract_quarter, sort: egen NCustomers = count(account_id)


	collapse (mean) network green_plan mean_weighted discounted_price module price* year NCustomers cum new *_interp unitprice houseprice area *ROOF*  (firstnm) TotalH_2006 (sum) NGreen=green_plan , by(postcode contract_quarter)

	
	by postcode, sort: egen Nobs = count(postcode)
	keep if Nobs>1
	

	
****************************
****** RECODE EVENTS *******
****************************	

	gen SHCP_endtime = -1*(contract_quarter  - tq(2009, q3))
	gen PFIT_starttime = contract_quarter  - tq(2009, q4)
	gen PFIT_endtime = -1*(contract_quarter  - tq(2012, q1))
	gen SRES1_endtime =  -1*(contract_quarter  - tq(2011, q3))
	gen SRES2_endtime =  -1*(contract_quarter  - tq(2012, q3))
	gen SRES_TFIT_endtime = -1*(contract_quarter  - tq(2013, q1))
	gen FFIT_endtime = -1*(contract_quarter  - tq(2014, q1))


	gen quarter = contract_quarter
	
	gen premium_green = price_green - price_nongreen

	label variable median_income_interp "Median Income (AUD 000s)"
	label variable median_mortgage_interp "Median Mortgage Payment (AUD 000s)"
	label variable median_rent_interp "Median Rental Payment (AUD 000s)" 
	label variable PBachelor_interp "Proportion Bachelor's Degree"
	label variable PRented_interp "Proportion Rental Properties"
	label variable median_rooms_interp "Median House Size"
	label variable PFullTime_interp "Proportion Employed Full Time"
	label variable median_age_interp "Median Age"
	
	
	
	label variable PROOFCO "% Roof Concrete"
	label variable PROOFFI "% Roof Fibre Cement"
	label variable PROOFIM "% Roof Imitation Tile"
	label variable PROOFME "% Roof Metal Sheeting"
	label variable PROOFSY "% Roof Synthetic"
	label variable PROOFTI "% Roof Tile"
	
	xtset postcode contract_quarter
	
save Data/event_study_sample_quartsince2006.dta, replace












